package com.example.springbootdemolianxi002.service;

import com.example.springbootdemolianxi002.entity.Task;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class TaskService {
    //新建一个新的集合来装这些任务列表
    List<Task>  tasks = new ArrayList<>();

    int  nextId = 9;
    //先给这个提供一些原始数据

    public TaskService(){

        tasks.add(new Task(1,"打到日本帝国主义","经常性的反思自己",false));
        tasks.add(new Task(2,"学回Java开发","学Java开发创业",false));
        tasks.add(new Task(3,"打得一拳开免得百拳来","人无完人",false));
        tasks.add(new Task(4,"打倒日本帝国主义","怎么能让自己变得更强?",false));
        tasks.add(new Task(5,"找到Java工作","经常性的反思自己",false));
        tasks.add(new Task(8,"从新开始从头开始自己的人生","掌控多巴胺掌控命运",false));


    }
    //实现getTask方法，并且防御性质编程
    public ArrayList<Task>  getallTask(){
        return  new ArrayList<>(tasks);
    }

    //实现getBYID方法,并且防御性编程

    public Task  getTaskByID(int  id){
        return  tasks.stream()
                .filter(task -> task.getId() == id)
                .findAny()
                .orElse(null);
    }

    //实现增加功能

    public Task  createTTask(Task task){
        task.setId(nextId++);
        tasks.add(task);
        return task;


    }

    //修改功能

    public Task updateTask(int id,Task task){
            Task  Atask = getTaskByID(id);
            if(Atask != null){
                Atask.setId(task.getId());
                Atask.setName(task.getName());
                Atask.setDescription(task.getDescription());
                Atask.setCompleted(task.getCompleted());
                return  Atask;
            }
        return  null;

    }

    //删除功能
    public boolean  deleteTask(int  id){

        return tasks.removeIf(task -> task.getId() == id);
    }

    //标记任务已经完成
    public Task markandCompleted(int  id){
        Task  task =getTaskByID(id);
        if(task  != null){
            task.setCompleted(true);
            return task;
        }
return   null;
    }
}
